{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3854717a",
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################数 组 去 重#####################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "43d21b4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "print ('第一个数组：')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "34ab547c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组的去重值：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([2, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print ('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "188916a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去重数组的索引数组：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 4 7 9]\n"
     ]
    }
   ],
   "source": [
    "print ('去重数组的索引数组：')\n",
    "u,indices = np.unique(a, return_index = True)#返回新列表元素在旧列表中的位置（下标）,并以列表形式储\n",
    "print(u)\n",
    "print(indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c57f5ebf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n"
     ]
    }
   ],
   "source": [
    "print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "print (a)\n",
    "print ('去重数组的下标：')\n",
    "b,indices = np.unique(a, return_inverse = True)#旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "print (b)\n",
    "print (indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e7b0d8ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "source": [
    "print ('返回去重元素的重复数量：')\n",
    "u,indices = np.unique(a, return_counts = True)#返回去重数组中的元素在原数组中的出现次数,并以列表形式储\n",
    "print (u)\n",
    "print(indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2c734051",
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################数 组 添 加#####################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "62d45eca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第二个数组：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]]) \n",
    "print ('第二个数组：')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "68be4d53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向数组添加元素:\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[1 2 3 4 5 6 9 8 7]\n"
     ]
    }
   ],
   "source": [
    "print('向数组添加元素:')  #当 axis 无定义时，是横向加成，返回总是为一维数组！\n",
    "a1=np.append(a,[9,8,7]) #返回一个新的一维数组，\n",
    "print(a)\n",
    "print(a1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "e348a96a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [9 8 7]]\n"
     ]
    }
   ],
   "source": [
    "print('沿轴 0 添加元素：')        #当 axis 为 0 的时候（列数要相同）\n",
    "a2 = np.append(a,[[9,8,7]],axis=0) #向二维数组添加元素，添加的数组也必须是二维数组\n",
    "print(a)\n",
    "print(a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "89d42c23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 添加元素：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 5],\n",
       "       [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('沿轴 1 添加元素：')\n",
    "a3 = np.append(a , [[5],[7]] ,axis=1) #当 axis 为 1 时，数组是加在右边（行数要相同）\n",
    "a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "2d1e7a57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "[ 1  2  3  4  5 11 12  6]\n",
      "[ 1  2  3  4  5 11 12  6]\n",
      "[ 1  2  3  4  5  6 11 12]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print(np.insert(a, 5, [11, 12]))\n",
    "print(np.insert(a, -1, [11, 12]))\n",
    "print(np.insert(a, 6, [11, 12]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c8c0457c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [12 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "[[ 1  2]\n",
      " [13 13]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n"
     ]
    }
   ],
   "source": [
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "print(a)\n",
    "print('沿轴 0 广播：')\n",
    "print(np.insert(a, 1, [12], axis=0)) #广播插入的12\n",
    "#不带“[]”\n",
    "print(np.insert(a, 1, 13, axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "268c463d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 广播：\n",
      "[[ 1  2 11]\n",
      " [ 3  4 11]\n",
      " [ 5  6 11]]\n"
     ]
    }
   ],
   "source": [
    "print('沿轴 1 广播：')\n",
    "print(np.insert(a, 2, 11, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "54c4bb24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('原数组：')\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f81f0c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################数 组 删 除#####################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "3d9e7068",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "07ef9b6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "q1=np.delete(a,5)\n",
    "q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "16351ea9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除每一行中的第二列：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  3],\n",
       "       [ 4,  5,  7],\n",
       "       [ 8,  9, 11]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('删除每一行中的第3列：')\n",
    "q2=np.delete(a , 2 ,axis=1)\n",
    "q2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "3982c0e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除每一列中的第2行：\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('删除每一列中的第2行：')\n",
    "q3 = np.delete(a,1,axis=0)\n",
    "q3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
